/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Server.Database.Database;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author sarunaskujalis
 */
public class AdminWindowAutomatics extends javax.swing.JPanel {
    
    private Database db;
    /**
     * Creates new form AdminWindowAutomatics
     */
    public AdminWindowAutomatics(Database db) {
        this.db = db;
        initComponents();
        setUpTab();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        serverResetInput = new javax.swing.JTextField();
        serverShutDownInput = new javax.swing.JTextField();
        saveAutomaticsBtn = new javax.swing.JButton();

        jLabel1.setText("Perkrauti serverį kas:");

        jLabel2.setText("Išjungti serverį:");

        jLabel3.setText("val.");

        jLabel4.setText("(h:min)");

        jLabel5.setText("*jei norite, kad atitinkami veiksmai nebūtų vykdomi, laukelius palikite tuščius.");

        serverResetInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverResetInputActionPerformed(evt);
            }
        });

        serverShutDownInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverShutDownInputActionPerformed(evt);
            }
        });

        saveAutomaticsBtn.setText("Išsaugoti");
        saveAutomaticsBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                handleSaveBtn(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(44, 44, 44)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(serverResetInput)
                            .add(serverShutDownInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 255, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel3))))
                .addContainerGap(162, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(saveAutomaticsBtn)
                .add(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(107, 107, 107)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(serverResetInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .add(2, 2, 2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(serverShutDownInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(jLabel4))
                .add(18, 18, 18)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 99, Short.MAX_VALUE)
                .add(saveAutomaticsBtn)
                .add(23, 23, 23))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void serverShutDownInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverShutDownInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_serverShutDownInputActionPerformed

    private void serverResetInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverResetInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_serverResetInputActionPerformed

    private void handleSaveBtn(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_handleSaveBtn
        String restart = serverResetInput.getText();
        String shutdown = serverShutDownInput.getText();
        try {
            if((validateTime(shutdown) || shutdown.isEmpty() )&& (validateNum(restart) || restart.isEmpty())){
                db.settingsRegister.setSettings("RESTART", restart);
                db.settingsRegister.setSettings("SHUTDOWN", shutdown);
            } else {
                 JOptionPane.showMessageDialog(null,
                                "Įvestas laikas nėra validus.", //"+restart+":"+validateTime(restart)+" "+shutdown+": "+validateNum(shutdown)
                                "Klaida",
                                JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowAutomatics.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_handleSaveBtn
    
    private void setUpTab() {
        String restart = "";
        String shutdown = "";
        
        try {
            restart = db.settingsRegister.getSettingValue("RESTART");
            shutdown = db.settingsRegister.getSettingValue("SHUTDOWN");
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowAutomatics.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        serverResetInput.setText(restart);
        serverShutDownInput.setText(shutdown);
    }
    public boolean validateTime(String time){
        Pattern pattern = Pattern.compile("([01]?[0-9]|2[0-3]):[0-5][0-9]");
        Matcher matcher = pattern.matcher(time);
        return matcher.matches();

   }
   public boolean validateNum(String num){
       try{
           int n = Integer.parseInt(num);
           if(n > 0)
               return true;
           else
               return false;
       } catch(Exception e){
           return false;
       }
   }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton saveAutomaticsBtn;
    private javax.swing.JTextField serverResetInput;
    private javax.swing.JTextField serverShutDownInput;
    // End of variables declaration//GEN-END:variables
}
